package ggze.mps.home.controller;

import ggze.actuator.ActuatorController;
import ggze.mps.home.dao.inter.RoleDao;
import ggze.mps.home.dao.inter.Role_NavigationDao;
import ggze.mps.home.dao.inter.Role_ViewDao;
import ggze.mps.home.entity.Role;
import ggze.mps.home.entity.Role_Navigation;
import ggze.mps.home.entity.Role_View;
import ggze.tool.BeanToJson;
import ggze.tool.ReturnBody;
import ggze.tool.TimeUtils;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;
import java.util.List;

@RestController
@RequestMapping("RoleController")
public class RoleController {
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RoleDao RoleDaoName;
    @Autowired
    private Role_NavigationDao role_navigationDao;
    @Autowired
    private ActuatorController actuatorController;
    @Autowired
    private Role_ViewDao role_viewDao;
    @RequestMapping("add")
    public String add() throws UnsupportedEncodingException {
        Role entity = (Role) BeanToJson.jsonTobean(request.getParameter("jon"), Role.class);
        ReturnBody returnBody = new ReturnBody();
        entity.setUuid("");
        try {
            String time = TimeUtils.getTimeFormat();
            entity.setCreateTime(time);
            entity.setMethodTime(time);
            actuatorController.execute(entity, "add", RoleDaoName);
            returnBody.setCode("0");
            returnBody.setTitle("成功");
        } catch (Exception e) {
            returnBody.setCode("1");
            returnBody.setTitle("系统错误");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }

    @RequestMapping("deleted")
    public String deleted() throws UnsupportedEncodingException {
        Role entity = (Role) BeanToJson.jsonTobean(request.getParameter("jon"), Role.class);
        ReturnBody returnBody = new ReturnBody();
        try {
            RoleDaoName.delete("uuid", entity.getUuid());
            returnBody.setCode("0");
            returnBody.setTitle("成功");
        } catch (Exception e) {
            returnBody.setCode("1");
            returnBody.setTitle("系统错误");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }

    @RequestMapping("update")
    public String update() throws UnsupportedEncodingException {
        Role entity = (Role) BeanToJson.jsonTobean(request.getParameter("jon"), Role.class);
        ReturnBody returnBody = new ReturnBody();
        try {
            String time = TimeUtils.getTimeFormat();
            entity.setMethodTime(time);
            actuatorController.execute(entity, "update", RoleDaoName);
            returnBody.setCode("0");
            returnBody.setTitle("成功");
        } catch (Exception e) {
            returnBody.setCode("1");
            returnBody.setTitle("系统错误");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }

    @RequestMapping("get")
    public String get() throws UnsupportedEncodingException {
        JSONObject jsonObject = JSONObject.fromObject(request.getParameter("jon"));
        ReturnBody returnBody = new ReturnBody();
        try {
            int begin = Integer.parseInt(jsonObject.getString("begin"));
            int end = Integer.parseInt(jsonObject.getString("end"));
            String zdname = jsonObject.getString("zdname");
            String zdval = jsonObject.getString("zdval");
            if ("1".equals(zdval)) {
                List<Role> entityLIST = RoleDaoName.paging(zdname, zdval, begin, end);
                returnBody.setData(BeanToJson.toJsonArrayString(entityLIST));
            } else {
                List<Role> entityLIST = RoleDaoName.customs("select * from Role where " + zdname + "  like '%" + zdval + "%'");
                returnBody.setData(BeanToJson.toJsonArrayString(entityLIST));
            }
            returnBody.setCode("0");
            returnBody.setTitle("成功");
        } catch (Exception e) {
            returnBody.setCode("1");
            returnBody.setTitle("系统错误");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }

    @RequestMapping("num")
    public String num() throws UnsupportedEncodingException {
        return String.valueOf(RoleDaoName.getNum("1", "1"));
    }

    @RequestMapping("seach")
    public String seach() {
        JSONObject jsonObject = JSONObject.fromObject(request.getParameter("jon"));
        ReturnBody returnBody = new ReturnBody();
        try {
            String roleCode = jsonObject.getString("roleCode");
            String roleName = jsonObject.getString("roleName");
            //String kehuName = jsonObject.getString("kehuName");

            String begin = jsonObject.getString("begin");
            String end = jsonObject.getString("end");
            StringBuilder where = new StringBuilder();
            where.append(" where 1=1 ");
            if (!StringUtils.isEmpty(roleCode)) {
                where.append(" and rolecode like '%" + roleCode + "%'");
            }
            if (!StringUtils.isEmpty(roleName)) {
                where.append(" and rolename like '%" + roleName + "%'");
            }

            String sqlCoum = "select count(*) from role " + where.toString();
            int coumsNum = RoleDaoName.customsNum(sqlCoum);


            where.append(" order by uuid limit " + begin + "," + end + "");
            String sqlMain = "select * from role " + where.toString();


            List<Role> oldCars = RoleDaoName.customs(sqlMain.toString());
            JSONObject res = new JSONObject();
            res.put("num", coumsNum);
            res.put("data", BeanToJson.toJsonArrayString(oldCars));
            returnBody.setCode("0");
            returnBody.setData(BeanToJson.toJsonObjectString(res));
        } catch (Exception e) {
            returnBody.setCode("1");
            returnBody.setTitle("");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }


    @RequestMapping("sq")
    public String sq() {
        JSONObject jsonObject = JSONObject.fromObject(request.getParameter("jon"));
        ReturnBody returnBody = new ReturnBody();
        try {

            String roleCode = jsonObject.getString("roleCode");
            String[] navigIds = jsonObject.getString("navigIds").split(",");
            role_navigationDao.delete("rolecode",roleCode);

            for (String navigId : navigIds) {
                Role_Navigation role_navigation = new Role_Navigation();
                role_navigation.setRoleCode(roleCode);
                role_navigation.setNavigationId(navigId);
                actuatorController.execute(role_navigation, "add", role_navigationDao);
            }
            Role role = RoleDaoName.isSelect("rolecode",roleCode);
            role.setRoleNum(String.valueOf(navigIds.length));
            role.setMethodTime(TimeUtils.getTimeFormat());
            actuatorController.execute(role, "update", RoleDaoName);
            returnBody.setCode("0");
            returnBody.setTitle("授权成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnBody.setCode("1");
            returnBody.setTitle("失败");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }

    @RequestMapping("sj")
    public String sj(){
        JSONObject jsonObject = JSONObject.fromObject(request.getParameter("jon"));
        ReturnBody returnBody = new ReturnBody();
        try {

            String roleCode = jsonObject.getString("roleCode");
            String[] viewIds= jsonObject.getString("viewIds").split(",");
            role_viewDao.delete("rolecode",roleCode);

            for (String viewId : viewIds) {
                Role_View role_view = new Role_View();
                role_view.setRoleCode(roleCode);
                role_view.setLookId(viewId);
                actuatorController.execute(role_view, "add", role_viewDao);
            }
            Role role = RoleDaoName.isSelect("rolecode",roleCode);
            role.setRoleNum(String.valueOf(viewIds.length));
            role.setMethodTime(TimeUtils.getTimeFormat());
            actuatorController.execute(role, "update", RoleDaoName);
            returnBody.setCode("0");
            returnBody.setTitle("授权成功");
        } catch (Exception e) {
            e.printStackTrace();
            returnBody.setCode("1");
            returnBody.setTitle("失败");
        }
        return BeanToJson.toJsonObjectString(returnBody);
    }
}
